package com.itheima.线程计数器;

import java.util.concurrent.CountDownLatch;
//必须等张总和赵总都来了，会议才能开始
public class Demo {
    public static void main(String[] args) throws InterruptedException {
        //1创建一个线程计数器
        CountDownLatch latch=new CountDownLatch(4);

        //创建张总线程并启动
        new Thread(()->{
            try {
                Thread.sleep(1000);
                System.out.println("张总来了");
                //计数器-1
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        //创建赵总线程并启动
        new Thread(()->{
            try {
                Thread.sleep(2000);
                System.out.println("赵总来了");
                //计数器-1
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        //创建吴总线程并启动
        new Thread(()->{
            try {
                Thread.sleep(3000);
                System.out.println("吴总来了");
                //计数器-1
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        //创建陆总线程并启动
        new Thread(()->{
            try {
                Thread.sleep(4000);
                System.out.println("陆总来了");
                //计数器-1
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();



        //3先等着  --当latch底层记录的等待线程数为0时，await就会被自动唤醒，继续往下指向
        latch.await();

        System.out.println("会议开始");

    }
}
